package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.ICompanyService;
import com.cn.wanxi.util.FileName;
import com.cn.wanxi.util.IFileName;
import com.cn.wanxi.util.IO;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务逻辑层
 * --
 * 以后一个方法里面如果做了多个事情，我们需要将方法拆分 拆分成最小单位
 * 如果一个方法里面代码大于80行左右就需要拆分
 */
public class CompanyServiceImpl implements ICompanyService {
//    private final String split = "\r\n";

    /**
     * 返回公司对象
     * 1.需要公司对象 创建一个新对实例化 没有值 空
     * 2.赋值  值从哪里来？
     * 3.文件来  创建文件
     * 4.写入数据
     * 5.读取数据
     * 6.封装数据
     *
     * @return
     */
    @Override
    public CompanyModel getCompapnyModel() {
/**
 * 所有正常的代码里面不应该出现固定的字符或者数字
 * 就应该提取出来：
 */
//        1.创建文件
//
        IO.createFile(IFileName.FILE_COMPANY);
//        2.写数据
//        2.5 得到数据
        List<String> data = getData();
        IO.writeData(data);
//        3.读数据
        String result = IO.readData();
//        4.封装数据
//        CompanyModel companyModel = new CompanyModel();
        return setData(result);
    }

    private List<String> getData() {
//        String[] aa = {"aa", "aa"};
//        List 集合
//        数组和集合的区别？
        CompanyDao companyDao = new CompanyDao();
//        泛型，尖括号里面的类型可以是任何的数据类型(引用类型)
        List<String> list = new ArrayList<>();
        list.add(companyDao.LOGO);
        list.add(CompanyDao.phone);
        list.add(CompanyDao.net);
        list.add(CompanyDao.email);
        list.add(CompanyDao.address);
        list.add(CompanyDao.linePhone);
        list.add(CompanyDao.version);
        return list;
    }

    private CompanyModel setData(String result) {
        System.out.println(result);
        CompanyModel companyModel = new CompanyModel();
//        根据写入数据时的分隔符来得到每一条数据,
        String[] results = result.split(FileName.FILE_SPLIT);
        companyModel.setLogo(results[0]);
        companyModel.setPhone(results[1]);
        companyModel.setNet(results[2]);
        companyModel.setEmail(results[3]);
        companyModel.setLinePhone(results[5]);
        companyModel.setAddress(results[4]);
        companyModel.setVersion(results[6]);
        return companyModel;
    }

//    /**
//     * 读取数据
//     *
//     * @param file
//     * @return
//     */
//    private String readData(File file) {
//        StringBuilder reuslt = new StringBuilder();
////        StringBuilder和StringBuffer和String的区别？
//        try {
////            InputStream inputStream = new FileInputStream(file);
//////            如果需要将所有数据全部读取完成，怎么办？
//
////            long length=file.length();
////            for (int i = 0; i < length; i++) {
//////                int read = inputStream.read();
////                reuslt+=(char) inputStream.read();
////            }
//
//            Reader reader = new FileReader(file);
////            long length = file.length();
////            for (int i = 0; i < length; i++) {
//////                int read = inputStream.read();
////                reuslt += (char) reader.read();
////            }
//            int rr = 0;
//            while ((rr = reader.read()) != -1) {
//                reuslt.append((char) rr);
//            }
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return String.valueOf(reuslt);
//    }

//    /**
//     * 写注释
//     *
//     * @param file
//     */
//    private void writeData(File file, List<String> data) {
//
//        try {
//            OutputStream outputStream = new FileOutputStream(file);
////            CompanyDao companyDao = new CompanyDao();
////            String[] aa = {};
//            for (int i = 0; i < data.size(); i++) {
//                outputStream.write(data.get(i).getBytes());
//                outputStream.write(split.getBytes());
//            }
//
////            outputStream.write(companyDao.LOGO.getBytes());
////            outputStream.write(split.getBytes());
////            outputStream.write(CompanyDao.phone.getBytes());
////            outputStream.write(split.getBytes());
////            outputStream.write(CompanyDao.net.getBytes());
////            outputStream.write(split.getBytes());
////            outputStream.write(CompanyDao.email.getBytes());
////            outputStream.write(split.getBytes());
////            outputStream.write(CompanyDao.address.getBytes());
////            outputStream.write(split.getBytes());
////            outputStream.write(CompanyDao.linePhone.getBytes());
////            outputStream.write(split.getBytes());
////            outputStream.write(CompanyDao.version.getBytes());
////            outputStream.write(split.getBytes());
//            outputStream.close();
////            outputStream.write(23421);
////            String aa="文";
////            System.out.println(aa.getBytes("ISO-8859-6").length);
////            String aa=null;
////            Integer bb=Integer.parseInt(aa);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 创建文件
     * File Java类 电脑里面文件或者文件夹的抽象定义
     * 文件和目录路径名的抽象表示
     */
//    private File createFile() {
//        String path = "";
//        File filePath = new File(path);
//        boolean exists = filePath.exists();
////        System.out.println(exists);
//        if (!exists) {
//            path = "E:\\wanxi\\step-3\\canyin03-0720\\web";
//            filePath = new File(path);
//        }
////        少些if else  可以if
////      创建包，文件夹
//        String directory = "note";
//        File fileDirectory = new File(filePath, directory);
//        if (!fileDirectory.exists()) {
//            boolean mkdir = fileDirectory.mkdirs();
//            System.out.println(mkdir);
//        }
//        String fileName = "company.txt";
//        File file = new File(fileDirectory, fileName);
//        try {
//            file.createNewFile();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        return file;
//    }
    public static void main(String[] args) {
        new CompanyServiceImpl().getCompapnyModel();
    }
}
